/**
 * @单位名称：飞羽个人 Copyright (c) 2017 All Rights Reserved.
 * @系统名称：测试
 * @工程名称：
 * @文件名称:
 * @类路径:
 */
package com.feiyu.service.impl;

import com.alibaba.druid.pool.DruidDataSource;
import com.feiyu.bean.HouseProject;
import com.feiyu.bean.HouseUnit;
import com.feiyu.common.service.impl.BaseServiceImpl;
import com.feiyu.common.util.CustomExcelUtil;
import com.feiyu.dao.HouseProjectMapper;
import com.feiyu.dao.HouseUnitMapper;
import com.feiyu.service.HouseProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;

/**
 *
 *
 * @see
 * @author nanshouxiao
 * @date 2018-5-16 9:20:13
 * @version V1.0.0
 */
@Service("houseProjectServiceImpl")
public class HouseProjectServiceImpl extends BaseServiceImpl<HouseProject, Integer> implements HouseProjectService {

    @Autowired
    private HouseProjectMapper houseProjectMapper;
    @Autowired
    private HouseUnitMapper houseUnitMapper;

    @Override
    public int batchSaveEntity(Collection<HouseProject> houseProjects) {
        return houseProjectMapper.batchSaveEntity(houseProjects);
    }

    @Override
    @Transactional
    public void importAllInfoByFilepath(String filePath, String batchNumber) {
        File file = new File(filePath);
        List<HouseProject> houseProjectList = CustomExcelUtil.handleProjectExcel(file);
        houseProjectList.forEach(houseProject -> houseProject.setBatchNumber(batchNumber));
        houseProjectMapper.batchSaveEntity(houseProjectList);

        String unitFileDir = filePath.substring(0, filePath.lastIndexOf("."));
        File unitDirFile = new File(unitFileDir);
        Set<String> fileNames = Arrays.stream(unitDirFile.listFiles()).filter(File::isFile).map(File::getName).collect(Collectors.toSet());
        List<HouseUnit> houseUnitList = new ArrayList<>();

        for (String fileName : fileNames) {
            Integer projectId = null;
            for (HouseProject houseProject : houseProjectList) {
                String orderIndex = houseProject.getOrderIndex();
                String projectName = houseProject.getProjectName();
                String buildingNumber = houseProject.getBuildingNumber();
                if (fileName.startsWith(orderIndex) && fileName.contains(buildingNumber)) {
                    projectId = houseProject.getId();
                    logger.info("项目 ：" + projectName + " 楼栋号：" + buildingNumber +  "存在对应文件：" );
                    break;
                }
            }

            File unitFile = new File(unitFileDir + File.separator + fileName);
            List<HouseUnit> thisUnit = CustomExcelUtil.handleUnitExcel(unitFile);
            for (HouseUnit houseUnit : thisUnit) {
                houseUnit.setFileName(fileName);
                houseUnit.setProjectId(projectId);
                houseUnit.setBatchNumber(batchNumber);
            }
            houseUnitList.addAll(thisUnit);

        }
        if (!houseUnitList.isEmpty()) {
            houseUnitMapper.batchSaveEntity(houseUnitList);
        }
    }
}